package com.example.bootdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bootdemo.model.Emp;
import com.example.bootdemo.service.EmpService;
import com.example.bootdemo.utils.Alioos;
import com.example.bootdemo.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@RestController
@RequestMapping(value = "emp", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class EmpController {
    @Autowired
    EmpService empService;

    @RequestMapping(value = "info", method = RequestMethod.GET)
    public R info(MultipartFile multipartFile) {

        Alioos.uploadFileAvatar(multipartFile);
        return R.ok();

    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public R list() {
        List<Emp> list = empService.list();

        System.out.println(list);
        System.out.println(list);
        System.out.println(list);
        return R.ok(list);
    }

    @RequestMapping(value = "list1", method = RequestMethod.POST)
    public R list1(@RequestBody Emp id) {
        System.out.println(id);
        List<Emp> list = empService.list();

        System.out.println(list);
        System.out.println(list);
        System.out.println(list);
        return R.ok(list);
    }

    @RequestMapping(value = "selecMypage", method = RequestMethod.GET)
    public R selecMypage() {
        // 条件构造器
        QueryWrapper<Emp> userQuery = Wrappers.query();

        Page<Emp> page = new Page<>(1, 3);

        IPage<Emp> userPage = empService.page(page, userQuery);
        System.out.println("总页数：" + userPage.getPages());
        System.out.println("总记录数：" + userPage.getTotal());

        List<Emp> userList = userPage.getRecords();
        userList.forEach(System.out::println);
        return R.ok(userList);
    }

    public static int count = 0;
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public  void add() {
        count++;
        log.info("count:{}", count);
    }

    public static AtomicInteger count1 = new AtomicInteger(0);
    @RequestMapping(value = "add1", method = RequestMethod.GET)
    public  void add1() {
        count1.incrementAndGet();
        log.info("count:{}", count1.get());
    }
    public static int count11 = 0;
    @RequestMapping(value = "barrier2", method = RequestMethod.GET)
    public R barrier2() throws Exception {
//        ExecutorService executor = Executors.newCachedThreadPool();
//
//        //  for (int i = 0; i < 10; i++) { }
//        final int threadNum = count11++;
//        Thread.sleep(1000);
//        executor.execute(() -> {
//            try {
//                race2(threadNum);
//            } catch (Exception e) {
//                log.error("exception", e);
//            }
//        });
//
//        executor.shutdown();
        return R.ok();

    }
    @RequestMapping(value = "barrier3", method = RequestMethod.GET)
    public R barrier3() throws Exception {


        //  for (int i = 0; i < 10; i++) { }
        final int threadNum = count11++;
            try {
                race2(threadNum);
            } catch (Exception e) {
                log.error("exception", e);
            }
        return R.ok();

    }
    @RequestMapping(value = "barrier4", method = RequestMethod.GET)
    public R barrier4() throws Exception {


        //  for (int i = 0; i < 10; i++) { }
        final int threadNum = count11++;
        try {
            race4(threadNum);
        } catch (Exception e) {
            log.error("exception", e);
        }
        return R.ok();

    }
    private  void race4(int threadNum) throws Exception {
        log.info("{} is ready", threadNum);

        cyclicBarrier.await();
        log.info("{} continue", threadNum);
        Emp user = new Emp();
        user.setName("DVF");
        user.setAge(threadNum);
        user.setEmail("dmy@baomidou.com");
        user.setManagerId(1L);
        boolean flag = user.insert();
        System.out.println(flag);
    }
    private  void race2(int threadNum) throws Exception {
        log.info("{} is ready", threadNum);
        cyclicBarrier.await();
        log.info("{} continue", threadNum);
    }
    CyclicBarrier cyclicBarrier = new CyclicBarrier(5,new Runnable() {
        @Override
        public void run() {
            System.out.println("5人已来,可以开席了");

        }
    });
}
